package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.impl.CourseContentServiceImpl;
import com.lagou.utils.DateUtils;
import lombok.ToString;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.Transient;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {
    /**
     * 展示对应课程的章节与课时信息
     * */
    public void findSectionAndLessonByCourseId(HttpServletRequest request , HttpServletResponse response){

        try {
            //1.获取参数
            String course_id = request.getParameter("course_id");
            //2.业务处理
            CourseContentService courseContentService =new CourseContentServiceImpl();
            List<Course_Section> sectionList = courseContentService.findSectionAndLessonByCourseId(Integer.parseInt(course_id));
            //3.返回结果
            String result= JSON.toJSONString(sectionList);

            response.getWriter().println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
        回显章节对应的课程信息
     *
     */
    public void findCourseById(HttpServletRequest request , HttpServletResponse response){
        try {
            //1.获取参数
            String courseId = request.getParameter("course_id");

            //2.业务处理 (创建service层  调用service里面的根据courseId查询的方法)
            CourseContentService courseContentService = new CourseContentServiceImpl();
            Course course = courseContentService.findCourseByCourseId(Integer.parseInt(courseId));
            //3.返回数据,将对象转换为JSON,只转换需要的字段
            SimplePropertyPreFilter filter= new SimplePropertyPreFilter(Course.class,"id","course_name");
            String result = JSON.toJSONString(course, filter);

            //返回结果
            response.getWriter().println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 保存&修改 章节信息
     * */
    public void saveOrUpdateSection(HttpServletRequest request , HttpServletResponse response){

        try {
            //1.获取参数 从域对象中获取
            Map<String,Object> map = (Map)request.getAttribute("map");
            //2.创建Course_Section
            Course_Section section = new Course_Section();
            //BeanUtils.populate(section,map);
            BeanUtils.copyProperties(section,map.get("section"));

            CourseContentService contentService = new CourseContentServiceImpl();
            //4.业务处理
            if(section.getId() == 0){
                //新增章节
                String result = contentService.saveSection(section);
                //5.响应结果
                response.getWriter().print(result);
            }else {
                //修改章节
                String result = contentService.updataSection(section);
                //5.响应结果
                response.getWriter().print(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 修改章节状态
     * */

    public void updateSectionStatus(HttpServletRequest request , HttpServletResponse response){

        try {
            //获取参数
            int id = Integer.parseInt(request.getParameter("id"));
            int status = Integer.parseInt(request.getParameter("status"));

            //获取service对象进行数据的处理（业务处理）
            CourseContentService courseContentService =new CourseContentServiceImpl();
            String result = courseContentService.updateSectionStatus(id, status);

            //返回获取的数值
            response.getWriter().println(result);
        } catch (IOException e) {
            e.printStackTrace();

        }
    }


    /**
        添加课时时   回显章节名称
     *
     */
    public void findSectionBySectionId(HttpServletRequest request , HttpServletResponse response){
        try {
            //1.获取参数
            String sectionId = request.getParameter("sectionId");
            //2.业务处理
            CourseContentService courseContentService = new CourseContentServiceImpl();
            //(获取了查询出来的section对象 )
            Course_Section section = courseContentService.findSectionBySectionId(Integer.parseInt(sectionId));

            //3.返回数据,将对象转换为JSON,只转换需要的字段
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course_Section.class,"course_id","section_name");

            //转成json格式并且 响应到客户端
            String result = JSON.toJSONString(section, filter);
            response.getWriter().print(result);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存&修改 课时信息
     *
     * */
    public void saveOrUpdateLesson(HttpServletRequest request , HttpServletResponse response){
        try {
            //1.获取参数 从域对象中获取
            Map<String,Object> map = (Map)request.getAttribute("map");

            //2.创建Course_Lesson
            Course_Lesson lesson = new Course_Lesson();
            //3.使用BeanUtils工具类,将map中的数据封装到 lesson

            BeanUtils.populate(lesson,map);

            //4.业务处理
            CourseContentService contentService = new CourseContentServiceImpl();

            if(lesson.getId() != 0){
                // 不等于0的时候，表示之前有数据 就是修改操作
                String result = contentService.updateLesson(lesson);
                //5.响应结果
                response.getWriter().print(result);

            }else {
                //否则 等于0 就是没有课时数据  就是新添加的 调用savaLesson方法
                String result = contentService.saveLesson(lesson);
                //5.响应结果
                response.getWriter().print(result);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
